﻿using JESAI.HttpRequestMonitor.Core.Abstracts;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace JESAI.HttpRequestMonitor.Process
{
    internal class DefaultHttpInvokeProcesser : IHttpInvokeProcesser
    {
        /// <summary>
        /// 传输层，用于将请求发送到服务器。
        /// </summary>
        public IRequestMonitorTransport RequestMonitorTransport { get; }

        /// <summary>
        /// 请求模型构建器，用于将请求对象转换为请求模型。
        /// </summary>
        public IRequestModelBuilder RequestModelBuilder { get; }

        /// <summary>
        /// 配置对象，用于读取配置文件。
        /// </summary>
        public IConfiguration Configuration { get; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="requestMonitorTransport"></param>
        /// <param name="requestModelBuilder"></param>
        /// <param name="configuration"></param>
        public DefaultHttpInvokeProcesser(IRequestMonitorTransport requestMonitorTransport, IRequestModelBuilder requestModelBuilder, IConfiguration configuration)
        {
            RequestMonitorTransport = requestMonitorTransport;
            RequestModelBuilder = requestModelBuilder;
            Configuration = configuration;
        }

        public void Process(HttpContext context, Stopwatch stopwatch)
        {
            var (requesetModel, requestDetailModel) = RequestModelBuilder.Build(context, stopwatch);

            if (requesetModel == null || requestDetailModel != null)
            {
                RequestMonitorTransport.Send(requesetModel, requestDetailModel);
            }
        }
    }
}
